package com.ostrich.cloud.ostrichUserService.app.controller;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ostrich.cloud.common.result.Result;
import com.ostrich.cloud.ostrichUserService.app.utils.GetParentId;
import com.ostrich.cloud.ostrichUserService.client.CusLocationMangerClient;
import com.ostrich.cloud.ostrichUserService.common.CustomerResultCode;
import com.ostrich.cloud.ostrichUserService.common.exception.CusException;
import com.ostrich.cloud.ostrichUserService.infra.model.CustomerLocation;
import com.ostrich.cloud.ostrichUserService.service.CustomerLocationService;
import com.ostrich.ostrichUserService.dto.CusLocationDto;
import com.ostrich.ostrichUserService.param.location.CusLocationParam;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 用户地址表 前端控制器
 * </p>
 *
 * @author Lucas
 * @since 2022-10-26
 */
@RestController
public class CustomerLocationController implements CusLocationMangerClient {

    @Resource
    private CustomerLocationService locationService;

    @Override
    public Result uploadLocation(CusLocationParam cusLocationParam) {
        Integer parentId = GetParentId.getParentId();
        cusLocationParam.setParentId(parentId);
        locationService.addLocation(cusLocationParam);
        return Result.ok();
    }

    @Override
    public Result getAllLocations() {
        Integer parentId = GetParentId.getParentId();
        List<CustomerLocation> list = locationService.list(new QueryWrapper<CustomerLocation>().eq("parent_id", parentId));
        if (list.size()==0){
            throw new CusException(CustomerResultCode.NOTHING_LOCATION_EXCEPTION);
        }
        List<CusLocationDto> cusLocationDto = BeanUtil.copyToList(list, CusLocationDto.class);
        return Result.ok(cusLocationDto);
    }

    @Override
    public Result removeCusLocation(Integer locationId) {
        locationService.removeCusLocation(locationId);
        return Result.okm("地址删除成功");
    }

    /**
     * 修改默认收获地址
     * @param locationId 地址id
     * @param status 状态
     * @return 标准
     */
    @Override
    public Result putCusLocationState(Integer locationId, String status) {
        Integer parentId = GetParentId.getParentId();
        locationService.putCusLocationState(locationId,status,parentId );
        return Result.okm("修改成功");
    }
}

